class Book {
  String? id;
  String isbn;
  String title;
  String author;
  String? publisher;
  String? publishDate;
  String? coverUrl;
  String? description;
  int? pageCount;
  String addedDate;

  Book({
    this.id,
    required this.isbn,
    required this.title,
    required this.author,
    this.publisher,
    this.publishDate,
    this.coverUrl,
    this.description,
    this.pageCount,
    required this.addedDate,
  });

  // 从JSON创建Book对象的工厂方法
  factory Book.fromJson(Map<String, dynamic> json) {
    return Book(
      id: json['id'],
      isbn: json['isbn'],
      title: json['title'],
      author: json['author'],
      publisher: json['publisher'],
      publishDate: json['publishDate']?.toDate(),
      coverUrl: json['coverUrl'],
      description: json['description'],
      pageCount: json['pageCount'],
      addedDate: json['addedDate']?.toDate() ?? DateTime.now(),
    );
  }

  // 将Book对象转换为JSON
  Map<String, dynamic> toJson() {
    return {
      'isbn': isbn,
      'title': title,
      'author': author,
      'publisher': publisher,
      'publishDate': publishDate,
      'coverUrl': coverUrl,
      'description': description,
      'pageCount': pageCount,
      'addedDate': addedDate,
    };
  }
}